////
//
/// \file   ./src/block_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/block_node.hpp>

#include <cassert>
#include <string>

#include <boost/shared_ptr.hpp>

#include <inc/parser/block_entry_node.hpp>

namespace inc {
namespace parser {

using boost::shared_ptr;
using std::string;

/// \brief  Default constructor
/// \author Chandler Carruth
/// \date   2008.05.16

BlockNode::BlockNode() {}

/// \brief  Empty destructor
/// \author Chandler Carruth
/// \date   2008.05.16

BlockNode::~BlockNode() {}

/// \brief  Parse an executable code block
/// \author Chandler Carruth
/// \date   2008.05.25
///
/// This parses out an executable code block consisting of any flow control
/// constructs, sub-blocks, and statements. It specifically introduces an
/// explicit sequencing to execution.

bool
BlockNode::parse( const iterator &first,
                  const iterator &last,
                  iterator &last_parsed )
{
  assert( get_text().empty() && entries.empty()
          && "This node was parsed more than once!" );

  iterator current = first;
  while( current < last )
  {
    boost::shared_ptr< BlockEntryNode > entry( new BlockEntryNode );
    if( !entry->parse( current, last, last_parsed ) )
      break;
    current = last_parsed;
    entries.push_back( entry );
  }

  if( !entries.empty() )
  {
    set_text( string( first, last_parsed ) );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

} //  end parser namespace
} //  end inc namespace
